/*
 * @Description: 路由文件
 * @Author: yilingsj（315800015@qq.com）
 * @Date: 2021-06-26 11:30:01
 * @LastEditors: liu (1427179517@qq.com)
 * @LastEditTime: 2022-01-22 18:25:07
 * @FilePath: \xiangmu-app\src\router\index.js
 */
// 路由
import { RouterMount, createRouter } from 'uni-simple-router'
import store from 'store'

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  // applet: {
  //   animationDuration: 300, // 默认 300ms  v2.0.6+
  // },
  // detectBeforeLock: (router, to, navType) => {
  //   // router.$lockStatus=false;
  //   console.log('路由锁 detectBeforeLock navType', navType, ';to', to, ';', ';', Date.now())
  // },
  routerErrorEach: ({ type, msg }) => {
    // ;当前路由', router.currentRoute,
    console.log('路由锁 routerErrorEach type=', type, ';msg=', msg, ';', Date.now())
    switch (type) {
      case 3: // APP退出应用
        // #ifdef APP-PLUS
        router.$lockStatus = false
        uni.showModal({
          title: '提示',
          content: '您确定要退出应用吗？',
          success: function (res) {
            if (res.confirm) {
              plus.runtime.quit()
            }
          },
        })
        // #endif
        // #ifdef MP
        router.$lockStatus = false
        router.pushTab({ name: 'index' })
        // #endif
        break
      case 2:
        router.$lockStatus = false
        break
      default:
        // 防止第一次点击后无法再触发点击
        setTimeout(() => {
          console.log('解锁呀')
          router.$lockStatus = false
          // 如果在权限页清空了缓存，强制跳登录
          // if (router.currentRoute.meta.auth && !store.state.user.isLogin) {
          //   router.replace({ name: 'login' })
          // }
        }, 100)
        break
    }
  },
  // 通配符，非定义页面，跳转404
  routes: [
    ...ROUTES,
    {
      path: '*',
      redirect: (to) => {
        return {
          name: '404',
        }
      },
    },
  ],
})

// 全局路由前置守卫
router.beforeEach((to, from, next) => {
  console.log('beforeEach=', to, ';', from, ';store.state=', store.state, ';ROUTES=', Date.now())
  // 权限控制登录
  // store.dispatch('showAuthModal')
  // this.$Router.push({ name: 'login' })
  if (to.meta) {
    // 弹窗提醒且未登录时
    if (to.meta.showModal && !store.state.user.isLogin) {
      uni.showModal({
        title: '提示',
        content: '该功能需要登录权限',
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击了确定按钮')
            // 强制指定跳转类型，防止tabBar导致无法跳转
            next({ name: 'login', NAVTYPE: 'push' })
          }
        },
      })
      uni.setStorageSync('toUrl', {
        name: to.name,
        query: to.query,
        type: to.NAVTYPE === 'push' ? 'replace' : to.NAVTYPE,
      })
      next(false)
    } else if (to.meta.auth && !store.state.user.isLogin) {
      // 登录权限时直接跳登录
      next({ name: 'login' })
    } else {
      // 其他情况正常访问
      console.log('其他情况正常访问')
      next()
    }
  } else {
    if (!store.state.user.isLogin) {
      console.log('无meta时且未登录')
      next({ name: 'login' })
    } else {
      next()
    }
    const query = { ...to.params, ...to.query }
    // next({ path: to.path })
  }
})

export { router, RouterMount }
